import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    Queue<Integer> qu1 =new LinkedList<>();
    Queue<Integer> qu2 =new LinkedList<>();
    public MyStack() {

    }

    public void push(int x) {
        if(empty()){
            qu1.offer(x);
        }else{
            if(!qu1.isEmpty()){
                qu1.offer(x);
            }else qu2.offer(x);
        }
    }

    public int pop() {
        if(empty()) return -1;
        else{
            if(!qu1.isEmpty()){
                int i=qu1.size()-1;
                while(i>0){
                    qu2.offer(qu1.poll());
                    i--;
                }
                return qu1.poll();
            }
            else{
                int i=qu2.size()-1;
                while(i>0){
                    qu1.offer(qu2.poll());
                    i--;
                }
                return qu2.poll();
            }
        }
    }

    public int top() {
        if(empty()) return -1;
        else{
            if(!qu1.isEmpty()){
                int i=qu1.size()-1;
                while(i>0){
                    qu2.offer(qu1.poll());
                    i--;
                }
                int q=qu1.peek();
                qu2.offer(qu1.poll());
                return q;
            }
            else{
                int i=qu2.size()-1;
                while(i>0){
                    qu1.offer(qu2.poll());
                    i--;
                }
                int q=qu2.peek();
                qu1.offer(qu2.poll());
                return q;
            }
        }
    }

    public boolean empty() {
        if(qu1.isEmpty()&&qu2.isEmpty())
            return true;
        return false;
    }
}
